package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseShop;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBaseShopService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工厂模型Controller
 *
 * @author ruoyi
 * @date 2023-02-02
 */
@Api(tags = "工厂模型api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/shop" )
public class BaseShopController extends BaseController {

    private final IBaseShopService iBaseShopService;

    /**
     * 查询工厂模型列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseShop baseShop)
    {
        startPage();
        LambdaQueryWrapper<BaseShop> lqw = new LambdaQueryWrapper<BaseShop>();
        if (baseShop.getShopId() != null){
            lqw.eq(BaseShop::getShopId ,baseShop.getShopId());
        }
        if (baseShop.getParentId() != null){
            lqw.eq(BaseShop::getParentId ,baseShop.getParentId());
        }
        if (StringUtils.isNotBlank(baseShop.getAncestors())){
            lqw.eq(BaseShop::getAncestors ,baseShop.getAncestors());
        }
        if (StringUtils.isNotBlank(baseShop.getShopName())){
            lqw.like(BaseShop::getShopName ,baseShop.getShopName());
        }
        if (baseShop.getOrderNum() != null){
            lqw.eq(BaseShop::getOrderNum ,baseShop.getOrderNum());
        }
        if (StringUtils.isNotBlank(baseShop.getShopCode())){
            lqw.eq(BaseShop::getShopCode ,baseShop.getShopCode());
        }
        if (StringUtils.isNotBlank(baseShop.getStatus())){
            lqw.eq(BaseShop::getStatus ,baseShop.getStatus());
        }
        List<BaseShop> list = iBaseShopService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出工厂模型列表
     */
    @Log(title = "工厂模型" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseShop baseShop) {
        LambdaQueryWrapper<BaseShop> lqw = new LambdaQueryWrapper<BaseShop>(baseShop);
        List<BaseShop> list = iBaseShopService.list(lqw);
        ExcelUtil<BaseShop> util = new ExcelUtil<BaseShop>(BaseShop. class);
        return util.exportExcel(list, "shopConfig" );
    }

    /**
     * 获取工厂模型详细信息
     */
    @GetMapping(value = "/{shopId}" )
    public AjaxResult getInfo(@PathVariable("shopId" ) Long shopId) {
        return AjaxResult.success(iBaseShopService.getById(shopId));
    }

    /**
     * 新增工厂模型
     */
    @Log(title = "工厂模型" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseShop baseShop) {
        List<BaseShop> list = iBaseShopService.selectShopList(baseShop);
        if(list.size() > 0){
            return AjaxResult.error(baseShop.getShopCode() + "'编码已存在");
        }
        return toAjax(iBaseShopService.save(baseShop) ? 1 : 0);
    }

    /**
     * 修改工厂模型
     */
    @Log(title = "工厂模型" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseShop baseShop) {
        return toAjax(iBaseShopService.updateById(baseShop) ? 1 : 0);
    }

    /**
     * 删除工厂模型
     */
    @Log(title = "工厂模型" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{shopIds}" )
    public AjaxResult remove(@PathVariable Long[] shopIds) {
        return toAjax(iBaseShopService.removeByIds(Arrays.asList(shopIds)) ? 1 : 0);
    }


    /**
     * 获取工厂模型下拉树列表
     */
    @ApiOperation(value = "获取工厂模型下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(BaseShop shop)
    {
        List<BaseShop> shops = iBaseShopService.selectShopList(shop);
        return AjaxResult.success(iBaseShopService.buildShopTreeSelect(shops));
    }

    /**
     * 根据工厂类型获取树节点
     */
    @ApiOperation(value = "根据工厂类型获取树节点")
    @GetMapping("/getTreeFindType/{shopType}")
    public AjaxResult getTreeFindType(@PathVariable String shopType)
    {
        List<BaseShop> shops = iBaseShopService.getTreeFindType(shopType);
        return AjaxResult.success(shops);
    }

    /**
     * 根据下级节点查询出上级节点信息
     */
    @ApiOperation(value = "根据下级节点查询出工厂上级节点信息")
    @GetMapping("/getLevel/{shopCode}")
    public AjaxResult getLevel(@PathVariable String shopCode)
    {
        BaseShop shop = iBaseShopService.getLevel(shopCode);
        return AjaxResult.success(shop);
    }

    /**
     * 根据工厂id获取下级节点列表
     */
    @ApiOperation(value = "根据工厂id获取下级节点列表")
    @GetMapping("/getTreeFindId/{shopId}")
    public AjaxResult getTreeFindId(@PathVariable Integer shopId)
    {
        List<BaseShop> shops = iBaseShopService.getTreeFindId(shopId);
        return AjaxResult.success(shops);
    }


    /**
     * 获取工厂模型二级节点列表
     */
    @ApiOperation(value = "获取工厂模型二级节点列表")
    @RequestMapping(value = "/getTreeList", method = RequestMethod.POST)
    public AjaxResult getTreeList(@RequestBody BaseShop shop)
    {
        LambdaQueryWrapper<BaseShop> lqw = new LambdaQueryWrapper<BaseShop>();
        if(null!=shop.getShopId()){
            lqw.eq(BaseShop::getParentId,shop.getShopId());
        }
        List<BaseShop> list = iBaseShopService.list(lqw);
        return AjaxResult.success(list);
    }


}
